﻿package mortal.game.view.forging.view
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.forging.*;
    import mortal.game.view.forging.data.*;
    import mortal.game.view.forging.util.*;
    import mortal.game.view.palyer.*;
    import mortal.game.view.shop.*;
    import mortal.game.view.systemSetting.*;
    import mortal.mvc.core.*;

    public class GemInfo extends GSprite
    {
        private var _item:BaseItem;
        private var _tipTxt:GTextFiled;
        private var _txtStrenLv:BitmapNumberText;
        private var _smallIcon:GImageBitmap;
        private var _gemData:ItemData;
        private var _index:int;
        private var _isSelected:Boolean;
        private var _isOpen:Boolean;
        private var _selectBg:ScaleBitmap;
        private var _upgradeBtn:GLoadingButton;
        private var _buyGemBtn:GLoadingButton;
        private var _embedGemBtn:GLoadingButton;
        private var _advanceGemBtn:GLoadingButton;
        private var _data:GemInfoData;
        private var _needNum:int;
        private var _getItemCallBack:Function;

        public function GemInfo()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._item = UICompomentPool.getUICompoment(BaseItem);
            this._item.setItemStyle(ItemStyleConst.Small, ImagesConst.GemStreng_ItemBg, 5, 5);
            this._item.isDragAble = false;
            this._item.mouseEnabled = true;
            this._item.buttonMode = true;
            this._item.isShowLock = false;
            this.addChild(this._item);
            this._tipTxt = UIFactory.gTextField("", 8, 8 - 3, 40, 34 + 5, this, GlobalStyle.textFormatHui);
            this._tipTxt.multiline = true;
            this._tipTxt.wordWrap = true;
            this._tipTxt.mouseEnabled = false;
            this._tipTxt.autoSize = TextFieldAutoSize.LEFT;
            this._txtStrenLv = UIFactory.bitmapNumberText(0, 0, "FightInfoNum.png", 12, 15, -1, this);
            this._smallIcon = UIFactory.gImageBitmap("", 7 - 3, 9 - 6, this);
            this._smallIcon.configEventListener(MouseEvent.CLICK, this.onBtnClickHandler);
            this._selectBg = UIFactory.bg(-2, -2, 50, 50, this, ImagesConst.taskLightMask);
            this._selectBg.visible = false;
            this._buyGemBtn = UIFactory.gLoadingButton(ResFileConst.BuyGemBtn, -28, 52 - 2 - 4, 100, 29, this);
            this._buyGemBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._buyGemBtn.visible = false;
            this._embedGemBtn = UIFactory.gLoadingButton(ResFileConst.EmbedGemBtn, -28, 52 - 2 - 4, 100, 29, this);
            this._embedGemBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._embedGemBtn.visible = false;
            this._upgradeBtn = UIFactory.gLoadingButton(ResFileConst.GemUpgradeBtn, -28, 52 - 2 - 4, 100, 29, this);
            this._upgradeBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._upgradeBtn.visible = false;
            this._advanceGemBtn = UIFactory.gLoadingButton(ResFileConst.AdvanceGemBtn, -28, 52 - 2 - 4, 100, 29, this);
            this._advanceGemBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._advanceGemBtn.visible = false;
            this.mouseEnabled = true;
            this._item.doubleClickEnabled = true;
            this._item.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this.addListeners();
            return;
        }// end function

        private function addListeners() : void
        {
            return;
        }// end function

        private function removeListeners() : void
        {
            return;
        }// end function

        private function onBtnClickHandler(event:MouseEvent) : void
        {
            var _loc_4:int = 0;
            var _loc_2:* = this._item.localToGlobal(new Point(this._item.x + this._item.width));
            var _loc_3:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            if (_loc_3 == null)
            {
                return;
            }
            if (_loc_3.itemData)
            {
                _loc_4 = _loc_3.itemData.itemInfo.type;
                GemSelWin.instance.show(_loc_2.x, _loc_2.y);
                GemSelWin.instance.data = _loc_3.itemData;
            }
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ForgingOnSelGem, this._index));
            var _loc_2:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            if (_loc_2 == null)
            {
                MsgManager.showRollTipsMsg(Language.getString(41147), MsgRollTipsType.msgRollTips3);
                return;
            }
            if (_loc_2.itemData)
            {
                if (!this._isOpen)
                {
                    MsgManager.showRollTipsMsg(Language.getString(41148), MsgRollTipsType.msgRollTips3);
                    return;
                }
                if (event.target == this._buyGemBtn)
                {
                    this.onGemBuy();
                }
                if (event.target == this._embedGemBtn)
                {
                    this.onGemEmbed();
                }
                if (event.target == this._upgradeBtn)
                {
                    this.onGemUpgrade();
                }
                if (event.target == this._advanceGemBtn)
                {
                    this.onGemAdvance();
                }
                if ((this._data.operType == GemOperType.OperType_CanAdvance || this._data.operType == GemOperType.OperType_CanUpgradeAndAdvance) && event.target == this._item)
                {
                    this.onGemAdvance();
                }
            }
            return;
        }// end function

        private function onGemBuy() : void
        {
            var _loc_2:TEquipJewelMatch = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:ItemInfo = null;
            var _loc_6:ForgingModule = null;
            var _loc_1:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            if (_loc_1 && _loc_1.itemData)
            {
                _loc_2 = EquipJewelMatchConfig.instance.getInfoByType(_loc_1.itemData.itemInfo.type);
                if (_loc_2)
                {
                    _loc_3 = _loc_2.jeweltype;
                    _loc_4 = GemUtil.getBaseGemCode(_loc_3);
                    _loc_5 = ItemConfig.instance.getConfig(_loc_4);
                    _loc_6 = GameController.forging.view as ForgingModule;
                    this.showGiftToUse(_loc_5, 1, this.onGemEmbed);
                }
            }
            return;
        }// end function

        private function onGemEmbed() : void
        {
            var _loc_1:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            var _loc_2:* = GemUtil.getPackAvailableGems(_loc_1.itemData);
            GemUtil.currOperType = GemOperType.OperType_CanEmbed;
            GameProxy.equip.jewelEmbedNew(_loc_1.itemData.uid, this._index, _loc_2[0].uid);
            return;
        }// end function

        private function onGemUpgrade() : void
        {
            var _loc_3:int = 0;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:TJewelUpdate = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:Dictionary = null;
            var _loc_10:Boolean = false;
            var _loc_11:int = 0;
            var _loc_12:ItemInfo = null;
            var _loc_13:int = 0;
            var _loc_14:Boolean = false;
            var _loc_1:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            var _loc_2:* = EquipJewelMatchConfig.instance.getInfoByType(_loc_1.itemData.itemInfo.type);
            if (_loc_2)
            {
                _loc_3 = _loc_2.jeweltype;
                _loc_4 = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(EGroup._EGroupStuff, EStuff._EStuffJewel, _loc_3);
                _loc_5 = 0;
                while (_loc_5 < _loc_4.length)
                {
                    
                    if (_loc_4[_loc_5].itemInfo.color > 2)
                    {
                        _loc_4.splice(_loc_5, 1);
                        _loc_5 = _loc_5 - 1;
                    }
                    _loc_5++;
                }
                _loc_4.sort(ItemsUtil.sortItem);
                _loc_6 = JewelUpdateConfig.instance.getInfoByColorAndLevel(this.data.gemData.itemInfo.color, (this.data.strengLevel + 1));
                if (!_loc_6)
                {
                    MsgManager.showRollTipsMsg(Language.getString(41149));
                    return;
                }
                if (!this.isCoinEnough())
                {
                    MsgManager.showRollTipsMsg(Language.getString(41150));
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShowBindCoinPackWin));
                    return;
                }
                _loc_7 = _loc_6.exp;
                _loc_9 = new Dictionary();
                _loc_5 = 0;
                while (_loc_5 < _loc_4.length)
                {
                    
                    _loc_8 = _loc_8 + (_loc_4[_loc_5] as ItemData).itemAmount;
                    _loc_9[(_loc_4[_loc_5] as ItemData).uid] = (_loc_4[_loc_5] as ItemData).itemAmount;
                    if (_loc_8 >= _loc_7)
                    {
                        _loc_9[(_loc_4[_loc_5] as ItemData).uid] = (_loc_4[_loc_5] as ItemData).itemAmount - _loc_8 + _loc_7;
                        break;
                    }
                    _loc_5++;
                }
                _loc_10 = ClientSetting.local.getIsDone(IsDoneType.AutoBuyGemProp);
                if (_loc_8 >= _loc_7)
                {
                    GameProxy.equip.jewelUpdate3(_loc_1.itemData.uid, this._index, _loc_9, _loc_10);
                    GemUtil.currOperType = GemOperType.OperType_CanUpgrade;
                }
                else
                {
                    _loc_11 = GemUtil.getBaseGemCode(_loc_3);
                    _loc_12 = ItemConfig.instance.getConfig(_loc_11);
                    _loc_13 = ShopConfig.instance.getFastBuyItemByCode(_loc_12.bind ? (_loc_12.codeUnbind) : (_loc_12.code)).prize * _loc_7;
                    _loc_14 = Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitGoldBind, _loc_13, null, false);
                    if (_loc_10)
                    {
                        if (_loc_14)
                        {
                            GameProxy.equip.jewelUpdate3(_loc_1.itemData.uid, this._index, _loc_9, _loc_10);
                            GemUtil.currOperType = GemOperType.OperType_CanUpgrade;
                        }
                        else
                        {
                            this.showGiftToUse(_loc_12, _loc_7 - _loc_8, this.onGemUpgrade);
                        }
                    }
                    else
                    {
                        this.showGiftToUse(_loc_12, _loc_7 - _loc_8, this.onGemUpgrade);
                    }
                }
            }
            return;
        }// end function

        private function showGiftToUse(param1:ItemInfo, param2:int, param3:Function) : void
        {
            var _loc_4:ForgingModule = null;
            if (GiftsUtil.isCanUseFreeGift(param1.group, param1.category, param1.type, int.MAX_VALUE, EGiftBag._EGiftBagFix, EGiftBag._EGiftBagMulSel))
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(41151, param2));
                return;
            }
            _loc_4 = GameController.forging.view as ForgingModule;
            if (BuyGiftItemWin.isViewShow && BuyGiftItemWin.isAutoBuy)
            {
                BuyGiftItemWin.isNeedTobuyCurTime = true;
                this._getItemCallBack = param3;
                NetDispatcher.addCmdListener(ServerCommand.BackPackItemsAdd, this.keepLevelUp);
            }
            else
            {
                MsgManager.showMouseMsg(Language.getStringByParam(41152, param2), MsgRollTipsType.msgRollTips3);
            }
            _loc_4 = GameController.forging.view as ForgingModule;
            BuyGiftItemWin.instance.setBuyData(param1.code, 1, param2, _loc_4.x + _loc_4.width + 5, _loc_4.y - 3);
            return;
        }// end function

        private function keepLevelUp(param1:Array) : void
        {
            var _loc_2:* = param1[0] as ItemData;
            if (ItemsUtil.isJewel(_loc_2))
            {
                NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsAdd, this.keepLevelUp);
                if (this._getItemCallBack != null)
                {
                    this._getItemCallBack.apply();
                }
                this._getItemCallBack = null;
            }
            return;
        }// end function

        private function onGemAdvance() : void
        {
            var _loc_3:int = 0;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:Dictionary = null;
            var _loc_1:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            var _loc_2:* = EquipJewelMatchConfig.instance.getInfoByType(_loc_1.itemData.itemInfo.type);
            if (_loc_2)
            {
                _loc_3 = _loc_2.jeweltype;
                _loc_4 = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(EGroup._EGroupStuff, EStuff._EPropJewelScroll, _loc_3);
                _loc_5 = 0;
                while (_loc_5 < _loc_4.length)
                {
                    
                    if (_loc_4[_loc_5].itemInfo.color <= this._data.gemData.itemInfo.color)
                    {
                        _loc_4.splice(_loc_5, 1);
                        _loc_5 = _loc_5 - 1;
                    }
                    _loc_5++;
                }
                _loc_4.sort(this.sortByColor);
                if (_loc_4.length > 0)
                {
                    _loc_6 = new Dictionary();
                    _loc_6[_loc_4[0].uid] = 1;
                    GameProxy.equip.JewelAdvance(_loc_1.itemData.uid, this._index, _loc_6);
                    GemUtil.currOperType = GemOperType.OperType_CanAdvance;
                }
                else
                {
                    MsgManager.showMouseMsg(Language.getString(41153), MsgRollTipsType.msgRollTips3);
                }
            }
            return;
        }// end function

        private function sortByColor(param1:ItemData, param2:ItemData) : int
        {
            if (param1.itemInfo.color > param2.itemInfo.color)
            {
                return -1;
            }
            if (param1.itemInfo.color < param2.itemInfo.color)
            {
                return 1;
            }
            return 0;
        }// end function

        private function onDBClickHandler(event:MouseEvent) : void
        {
            event.stopImmediatePropagation();
            return;
        }// end function

        public function set data(param1:GemInfoData) : void
        {
            this.clear();
            if (param1)
            {
                this._data = param1;
                this._isOpen = true;
                this._tipTxt.visible = false;
                if (this._data.operType == GemOperType.OperType_CanBuy)
                {
                    this._buyGemBtn.visible = true;
                }
                else if (this._data.operType == GemOperType.OperType_CanEmbed)
                {
                    this._embedGemBtn.visible = true;
                    EffectManager.glowFilterReg(this._embedGemBtn);
                }
                else if (this._data.operType == GemOperType.OperType_CanAdvance)
                {
                    this._smallIcon.imgUrl = ImagesConst.SmallIcon_CanAdvance;
                    this._advanceGemBtn.visible = true;
                    EffectManager.glowFilterReg(this._advanceGemBtn);
                }
                else if (this._data.operType == GemOperType.OperType_CanUpgrade)
                {
                    this._upgradeBtn.visible = true;
                    EffectManager.glowFilterReg(this._upgradeBtn);
                    this._upgradeBtn.toolTipData = this.getToolTipStr();
                    this._upgradeBtn.reactionTime = 200;
                }
                else if (this._data.operType == GemOperType.OperType_CanUpgradeAndAdvance)
                {
                    this._smallIcon.imgUrl = ImagesConst.SmallIcon_CanAdvance;
                    this._upgradeBtn.visible = true;
                    EffectManager.glowFilterReg(this._upgradeBtn);
                    this._upgradeBtn.toolTipData = this.getToolTipStr();
                    this._upgradeBtn.reactionTime = 200;
                }
                else
                {
                    this._upgradeBtn.visible = true;
                    this._upgradeBtn.toolTipData = this.getToolTipStr();
                    this._upgradeBtn.reactionTime = 200;
                }
                if (this._data.gemData)
                {
                    this._item.itemData = this._data.gemData;
                    if (this._data.operType == GemOperType.OperType_CanBuy)
                    {
                        this._item.isGray = true;
                    }
                    this.updateStrengLevel();
                }
                if (GemStrengthenPanel3.currSelGem && GemStrengthenPanel3.currSelGem.index == this._index)
                {
                    this.isSelected = true;
                }
            }
            return;
        }// end function

        public function get data() : GemInfoData
        {
            return this._data;
        }// end function

        private function updateStrengLevel() : void
        {
            if (this._data && this._data.strengLevel != 0)
            {
                this._txtStrenLv.text = "+" + this._data.strengLevel;
                this._txtStrenLv.x = 44 - this._txtStrenLv.width;
                this._txtStrenLv.y = 44 - this._txtStrenLv.height - 2;
            }
            else
            {
                this._txtStrenLv.text = "";
            }
            return;
        }// end function

        private function getToolTipStr() : String
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:ItemData = null;
            var _loc_1:String = "";
            var _loc_2:* = JewelUpdateConfig.instance.getInfoByColorAndLevel(this.data.gemData.itemInfo.color, (this.data.strengLevel + 1));
            if (_loc_2)
            {
                _loc_3 = _loc_2.exp;
                _loc_4 = _loc_3 * ForgingConst.GemUpgradeFee;
                _loc_5 = GemUtil.getBaseGemCode(this.data.gemData.itemInfo.type);
                _loc_6 = new ItemData(_loc_5);
                _loc_1 = Language.getStringByParam(41154, _loc_6.itemInfo.name, _loc_3, _loc_4);
            }
            return _loc_1;
        }// end function

        private function isCoinEnough() : Boolean
        {
            var _loc_1:Boolean = false;
            var _loc_3:int = 0;
            var _loc_2:* = JewelUpdateConfig.instance.getInfoByColorAndLevel(this.data.gemData.itemInfo.color, (this.data.strengLevel + 1));
            if (_loc_2)
            {
                _loc_3 = _loc_2.exp * ForgingConst.GemUpgradeFee;
                _loc_1 = Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitCoinBind, _loc_3);
            }
            return _loc_1;
        }// end function

        public function clear() : void
        {
            this._data = null;
            this._tipTxt.visible = true;
            this._item.itemData = null;
            this._item.isGray = false;
            this.isSelected = false;
            this._smallIcon.imgUrl = "";
            this._txtStrenLv.text = "";
            this._isOpen = false;
            EffectManager.glowFilterUnReg(this._upgradeBtn);
            EffectManager.glowFilterUnReg(this._embedGemBtn);
            EffectManager.glowFilterUnReg(this._advanceGemBtn);
            this._buyGemBtn.visible = false;
            this._embedGemBtn.visible = false;
            this._upgradeBtn.visible = false;
            this._advanceGemBtn.visible = false;
            return;
        }// end function

        private function resetState() : void
        {
            this._isOpen = false;
            return;
        }// end function

        public function get index() : int
        {
            return this._index;
        }// end function

        public function set index(param1:int) : void
        {
            var _loc_2:int = 0;
            this._index = param1;
            switch(param1)
            {
                case 1:
                {
                    _loc_2 = 56;
                    break;
                }
                case 2:
                {
                    _loc_2 = 56;
                    break;
                }
                case 3:
                {
                    _loc_2 = 80;
                    break;
                }
                case 4:
                {
                    _loc_2 = 100;
                    break;
                }
                case 5:
                {
                    _loc_2 = 110;
                    break;
                }
                case 6:
                {
                    _loc_2 = 120;
                    break;
                }
                default:
                {
                    break;
                }
            }
            this._tipTxt.text = _loc_2 + Language.getString(41155);
            return;
        }// end function

        public function get isSelected() : Boolean
        {
            return this._isSelected;
        }// end function

        public function set isSelected(param1:Boolean) : void
        {
            this._isSelected = param1;
            this._selectBg.visible = param1;
            return;
        }// end function

        public function get itemData() : ItemData
        {
            return this._item.itemData;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._item.dispose();
            this._item = null;
            this._tipTxt.dispose();
            this._tipTxt = null;
            this._txtStrenLv.dispose();
            this._txtStrenLv = null;
            this._gemData = null;
            this._index = 0;
            this._data = null;
            this._smallIcon.dispose();
            this._smallIcon = null;
            this._selectBg.dispose();
            this._selectBg = null;
            this._upgradeBtn.dispose();
            this._upgradeBtn = null;
            this._buyGemBtn.dispose();
            this._buyGemBtn = null;
            this._embedGemBtn.dispose();
            this._embedGemBtn = null;
            this._isSelected = false;
            this._isOpen = false;
            this._advanceGemBtn.dispose();
            this._advanceGemBtn = null;
            this._getItemCallBack = null;
            this.removeListeners();
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsAdd, this.keepLevelUp);
            super.disposeImpl();
            return;
        }// end function

    }
}
